//
//  LJPressScaleButton.m
//  LJAnimationsDemo
//
//  Created by raymond on 17/1/9.
//  Copyright © 2017年 raymond. All rights reserved.
//

#import "LJPressScaleButton.h"

@interface LJPressScaleButton ()

@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UILabel *label;


@property(nonatomic, assign) float precent;

@property(nonatomic, strong) CADisplayLink *link;

@end

@implementation LJPressScaleButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setupSubviews];
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CABasicAnimation *biggerAnim = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    biggerAnim.toValue = @0.9f;
    biggerAnim.removedOnCompletion = NO;
    biggerAnim.fillMode = kCAFillModeForwards;
    [self.backgroundView.layer addAnimation:biggerAnim forKey:@"bigger"];
    
    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(changeRichString)];
    [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    
    self.link = link;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.backgroundView.layer removeAllAnimations];
    [self.link invalidate];

}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.backgroundView.frame = CGRectMake(0, 0, self.width, self.height);
}

- (void)setupSubviews {
    [self addSubview:self.backgroundView];
    [self addSubview:self.label];
    
//    self.label.attributedText = [self precentStringWithPrecentValue:0.5];
}

- (void)changeRichString {
    if (self.precent >= 1) {
        return;
    }
    self.precent += 0.1;
    self.label.attributedText = [self precentStringWithPrecentValue:self.precent];
    self.backgroundView.backgroundColor = [UIColor colorWithRed:1-self.precent green:1-self.precent blue:1-self.precent alpha:1];
}

- (void)disappear {
    if (self.precent <= 1) {
        return;
    }
    self.precent -= 0.1;
    self.label.attributedText = [self precentStringWithPrecentValue:self.precent];
    self.backgroundView.backgroundColor = [UIColor colorWithRed:1-self.precent green:1-self.precent blue:1-self.precent alpha:1];
}

- (NSMutableAttributedString *)precentStringWithPrecentValue:(CGFloat)precent {
    NSString *string = [NSString stringWithFormat:@"%.f%%", fabs(precent * 100)];
    NSMutableAttributedString *richString = [[NSMutableAttributedString alloc] initWithString:string];
    
    [richString addAttribute:NSForegroundColorAttributeName
                       value:[[UIColor redColor] colorWithAlphaComponent:0.5]
                       range:NSMakeRange(richString.length-1, 1)];
    
    [richString addAttribute:NSForegroundColorAttributeName
                       value:[UIColor colorWithRed:precent green:precent blue:precent alpha:1.f]
                       range:NSMakeRange(0, richString.length-1)];
    
    [richString addAttribute:NSFontAttributeName
                       value:[UIFont fontWithName:@"HYQiHei-BEJF" size:30.f]
                       range:NSMakeRange(0, richString.length-1)];
    
    [richString addAttribute:NSFontAttributeName
                       value:[UIFont fontWithName:@"GillSans-LightItalic" size:14.f]
                       range:NSMakeRange(richString.length-1, 1)];
    
    return richString;
}

#pragma mark - lazy load

- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        _backgroundView.backgroundColor     = [UIColor whiteColor];
        _backgroundView.layer.cornerRadius  = self.backgroundView.width / 2.f;
        _backgroundView.layer.masksToBounds = YES;
    }
    
    return _backgroundView;
}

- (UILabel *)label {
    if (!_label) {
        _label = [[UILabel alloc] initWithFrame:self.bounds];
        _label.textAlignment = NSTextAlignmentCenter;
    }
    return _label;
}

@end
